package wps.component.elementsetting {

import mx.events.FlexEvent;

import spark.components.supportClasses.SliderBase;

import wps.util.EnableChecker;

public class SettingSliderBase extends SliderBase implements ISettingItem {

    //--------------------------------------------------------------------------
    //
    // Skin parts
    //
    //--------------------------------------------------------------------------

    [SkinPart(required = "true")]
    public var valueStepper:SettingNumericStepper;

    //--------------------------------------------------------------------------
    //
    // Properties
    //
    //--------------------------------------------------------------------------

    private var _target:Object;

    private var _targetProperty:String;

    private var _needCopy:Boolean = false;

    //----------------------------------
    // percentValueModel
    //----------------------------------

    private var _percentValueModel:Boolean = false;

    [Inspectable(category = "General", defaultValue = "false")]
    /**
     * 是否以百分数模式显示，默认false<br/>
     * 如果为true，实际值 = 显示值 / 100，显示值 = 实际值 ~~ 100
     * @default false
     */
    public function get percentValueModel():Boolean {
        return _percentValueModel;
    }

    public function set percentValueModel(percentValueModel:Boolean):void {
        _percentValueModel = percentValueModel;
    }

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    public function SettingSliderBase() {
        super();

        addEventListener(Event.CHANGE, changeHandler);
        addEventListener(FlexEvent.INITIALIZE, initializeHandler);
    }

    //--------------------------------------------------------------------------
    //
    // Methods
    //
    //--------------------------------------------------------------------------

    [Bindable]
    override public function get minimum():Number {
        return super.minimum;
    }

    override public function set minimum(value:Number):void {
        super.minimum = value;
    }

    [Bindable]
    override public function get maximum():Number {
        return super.maximum;
    }

    override public function set maximum(value:Number):void {
        super.maximum = value;
    }

    override public function set value(newValue:Number):void {
        super.value = newValue;

        // 直接设置value不会抛出change事件
        dispatchEvent(new Event(Event.CHANGE));
    }

    override protected function partAdded(partName:String, instance:Object):void {
        super.partAdded(partName, instance);

        if (instance == valueStepper) {
            // 必须在skin中设置min和max，否则子控件textDisplay控件的min和max值会不一致
            valueStepper.stepSize = snapInterval;

            // 把stepper与slider关联
            valueStepper.target = this;
            valueStepper.targetProperty = "value";
            valueStepper.needCopy = false;
        }
    }

    public function get needCopy():Boolean {
        return _needCopy;
    }

    public function set needCopy(needCopy:Boolean):void {
        _needCopy = needCopy;
    }

    [Bindable]
    public function get target():Object {
        return _target;
    }

    public function set target(target:Object):void {
        _target = target;
        targetChanged();
    }

    public function get targetProperty():String {
        return _targetProperty;
    }

    public function set targetProperty(targetProperty:String):void {
        _targetProperty = targetProperty;
        targetPropertyChanged();
    }

    public function get targetStyle():String {
        return null;
    }

    public function set targetStyle(targetStyle:String):void {

    }

    public function checkEnable():void {
        var checker:EnableChecker = new EnableChecker(this);
        enabled = checker.getEnableValue();
    }

    private function targetChanged():void {
        checkEnable();
        targetPropertyChanged();
    }

    private function targetPropertyChanged():void {
        if (target && targetProperty && enabled) {
            updateValue();
        }
        checkValue();
    }

    protected function updateValue():void {
        value = percentValueModel ? target[targetProperty] * 100 : target[targetProperty];
    }

    private function checkValue():void {
        value = enabled ? value : 0;
        value = isNaN(value) ? 0 : value;
    }

    //--------------------------------------------------------------------------
    //
    // Event handlers
    //
    //--------------------------------------------------------------------------

    protected function changeHandler(e:Event):void {
        if (target && enabled) {
            target[targetProperty] = percentValueModel ? value / 100 : value;

            // 偷懒写法，使blurY和blurX相同
            if (targetProperty == "blurX") {
                target["blurY"] = target["blurX"];
            }
        }
    }

    private function initializeHandler(e:FlexEvent):void {
        enabled = target ? true : false;
    }

}

}
